package com.juno.cafenoa;

import java.util.Iterator;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.juno.cafenoa.event.EventActivity;
import com.juno.cafenoa.util.CafeNoaPreference;
import com.juno.cafenoa.util.Util;

public class GCMIntentService extends GCMBaseIntentService {
	private static final String tag = "GCMIntentService";
	public static final String PROJECT_ID = "583693922537";
	
	// public 기본 생성자를 무조건 만들어야 한다.
	public GCMIntentService() {
		this(PROJECT_ID);
	}

	public GCMIntentService(String project_id) {
		super(project_id);
	}

	/** 푸시로 받은 메시지 */
	@Override
	protected void onMessage(Context context, Intent intent) {
		String name = intent.getStringExtra("EVENT_NAME");
		String content = intent.getStringExtra("EVENT_CONTENT");
		String stop = intent.getStringExtra("EVENT_STOP_TIME");
		
		Intent actionIntent = new Intent(context, MainActivity.class);
		actionIntent.putExtra(Constants.NOTIFICATION_START, true);
		PendingIntent pendingintent = PendingIntent.getActivity(context, 0, actionIntent, PendingIntent.FLAG_ONE_SHOT);
		Util.acquireCpuWakeLock(context);
		
		// R.drawable.ic_launcher는 임시적으로 이미지 적용.
		showNotification(context, R.drawable.main_icon, name, content+" , "+stop+"까지",	pendingintent, 0);
	}

	@Override
	protected void onError(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		Log.d(tag, "on_error. errorId : " + arg1);
	}

	/** 단말에서 GCM 서비스 등록 했을 때 등록 id를 받는다 */
	@Override
	protected void onRegistered(Context context, String regId) {
		Log.v(tag, "onRegistered. regId : " + regId);
		CafeNoaPreference.setGcmID(context, regId);
	}

	/** 단말에서 GCM 서비스 등록 해지를 하면 해지된 등록 id를 받는다 */
	@Override
	protected void onUnregistered(Context context, String regId) {
		Log.v(tag, "onUnregistered. regId : " + regId);
	}
	
	/**
	 * showNotification : Notification을 띄우기 위한 메소드.<br>
	 * @author JunHo 
	 */
	private void showNotification(Context context, int notifyImgResId, CharSequence title,
			CharSequence text, PendingIntent intentActivity, int notifyId) {
		Notification notification = new NotificationCompat.Builder(context).setContentTitle(title).setTicker(text)
				.setContentText(text).setSmallIcon(notifyImgResId).setDefaults(Notification.DEFAULT_ALL) 
				.setAutoCancel(true).addAction(notifyImgResId, "Action", intentActivity).build();
		notification.setLatestEventInfo(context, title, text, intentActivity);

		NotificationManager mNM = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
		mNM.notify(notifyId, notification);
		
		try {
			//3초간 정지후 cpulock을 해제함.
			Thread.sleep(1000 * 5);
			Util.releaseCpuLock();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}
